package co.com.travelsoft.ati.client;

import java.math.BigDecimal;
import java.util.Date;

import co.com.travelsoft.ati.dto.UsuarioDto;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.PasswordTextBox;

public class FormularioRegistroUsuario extends Composite {
	
	
	private final TravelSoftServiceAsync servicio = GWT
			.create(TravelSoftService.class);
	
	
	private FormPanel formulario = new FormPanel();
	private TextBox codigoTextBox = new TextBox();
	private TextBox loginTextBox = new TextBox();
	private TextBox nombreTextBox = new TextBox();
	private TextBox correoTextBox = new TextBox();
	private PasswordTextBox claveTextBox = new PasswordTextBox();
	private Button btnRegistrar = new Button("Registrar");

	public FormularioRegistroUsuario() {
		
		
		formulario.setMethod(FormPanel.METHOD_POST);
		formulario.setAction("/myFormHandler");
		initWidget(formulario);
		
		Grid grid = new Grid(6, 2);
		formulario.setWidget(grid);
		grid.setSize("100%", "100%");
		
		
		Label lblCodigo = new Label("Codigo");
		grid.setWidget(0, 0, lblCodigo);
		grid.setWidget(0, 1, codigoTextBox);
		
		
		Label lblLogin = new Label("Login");
		grid.setWidget(1, 0, lblLogin);
		grid.setWidget(1, 1, loginTextBox);
		
		Label lblNombre = new Label("Nombre");
		grid.setWidget(2, 0, lblNombre);
		grid.setWidget(2, 1, nombreTextBox);
		
		Label lblCorreoElectronico = new Label("Correo Electronico");
		grid.setWidget(3, 0, lblCorreoElectronico);
		grid.setWidget(3, 1, correoTextBox);
		
		Label lblClave = new Label("Clave");
		grid.setWidget(4, 0, lblClave);
		grid.setWidget(4, 1, claveTextBox);
		
		
		grid.setWidget(5, 1, btnRegistrar);
		
		btnRegistrar.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				formulario.submit();
			}
		});
		
		
		
		formulario.addSubmitHandler(new SubmitHandler() {
			
			@Override
			public void onSubmit(SubmitEvent event) {
				
				boolean valido = true;

				if(codigoTextBox.getText()==null || codigoTextBox.getText().trim().equals("")){
					valido=false;
				}
				if(loginTextBox.getText()==null || loginTextBox.getText().trim().equals("")){
					valido=false;
				}
				if(nombreTextBox.getText()==null || nombreTextBox.getText().trim().equals("")){
					valido=false;
				}
				if(claveTextBox.getText()==null || claveTextBox.getText().trim().equals("")){
					valido=false;
				}
				if(correoTextBox.getText()==null || correoTextBox.getText().trim().equals("")){
					valido=false;
				}
				
				if(!valido){
					event.cancel();	
				}
			}
		});
		
		formulario.addSubmitCompleteHandler(new SubmitCompleteHandler() {
			
			@Override
			public void onSubmitComplete(SubmitCompleteEvent event) {
				
				UsuarioDto usuario = new UsuarioDto();
				
				usuario.setCodigo(new BigDecimal(codigoTextBox.getText()));
				usuario.setLogin(loginTextBox.getText());
				usuario.setNombre(nombreTextBox.getText());
				usuario.setPassword(claveTextBox.getText());
				usuario.setCorreoelectronico(correoTextBox.getText());
				usuario.setUltimafechaingreso(new Date());
				
				
				servicio.registrarUsuario(usuario, new AsyncCallback<Boolean>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert(caught.getMessage());
					}

					@Override
					public void onSuccess(Boolean result) {
						if(result){
							Window.alert("Registro de usuario exitoso");
							History.newItem("login");
						}else{
							Window.alert("Login existe en el sistema");
						}
					}
				})	;		
			}
		});
		
	}

	
	
	

}
